<?php

namespace App\Http\Controllers;

use App\Common\Constant\Status;
use App\Common\Constant\Params;
use App\Http\Requests\ManagerAddPost;
use App\Http\Requests\ManagerEditPost;
use App\Libs\Filter\Filter;
use App\Models\Province;
use App\Services\IAreaService;
use App\Services\IManagerService;
use App\Services\ISystemService;
use App\User;
use Auth;
use Illuminate\Http\Request;

/**
 * 后台设置
 * Class AuthController
 * @package App\Http\Controllers
 */
class AdminController extends BaseController
{
    public function __construct()
    {
        $this->middleware('adminonly');
    }

    /**
     * 管理员列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function managerList(Request $request,IManagerService $managerService)
    {
        $filter = Filter::instance()
            ->HTML('用户名：')
            ->input('name')
            ->HTML('邮箱地址：')
            ->input('email')
            ->reset()->submit();
        if ($filter->is_filtering()){
            $list = $managerService->getManagerList($filter->get_where());
        }else{
            $list = $managerService->getManagerList();
        }
        return view('admin/managerList',['list'=>$list,'filter'=>$filter]);
    }

    /**
     * 新建
     * @param Request $request
     */
    public function add(ManagerAddPost $request,IManagerService $managerService)
    {
        if ($request->isMethod('post')){
            $managerService->add($request->input('name'),$request->input('email'),$request->input('password'));
        }
        return back();
    }

    /**
     * 启用
     * @param Request $request
     */
    public function enable($manager_id,Request $request)
    {

    }

    /**
     * 禁用
     * @param Request $request
     */
    public function disable($manager_id,Request $request)
    {

    }

    /**
     * 删除
     * @param Request $request
     */
    public function delete($manager_id,Request $request,IManagerService $managerService)
    {
        $managerService->delete($manager_id);
        return back();
    }

    /**
     * 修改
     * @param Request $request
     */
    public function edit(ManagerEditPost $request,IManagerService $managerService)
    {
        if ($request->isMethod('post')){
            $managerService->edit($request->input('manager_id'),$request->input('name'),$request->input('email'));
        }
        return back();
    }

    /**
     * 修改密码
     * @param Request $request
     */
    public function editPass(Request $request,IManagerService $managerService)
    {
        if ($request->isMethod('post') && $request->has('manager_id') && $request->has('password')){
            $managerService->modifyPassword($request->input('manager_id'),$request->input('password'));
        }
        return back();
    }

    /**
     * 短信日志列表
     * @param Request $request
     */
    public function smsLogList(Request $request,ISystemService $systemService)
    {
        $filter = Filter::instance()
            ->HTML('手机号：')
            ->input('sl_phone')
            ->HTML('发送时间：')
            ->date('sl_add_time')
            ->reset()->submit();
        if ($filter->is_filtering()){
            $list = $systemService->smsLogList($filter->get_where());
        }else{
            $list = $systemService->smsLogList();
        }
        return view('admin/smsLogList',['list'=>$list,'filter'=>$filter]);
    }

    /**
     * 省市区列表
     * @param Request $request
     * @param ISystemService $systemService
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function areaList(Request $request,IAreaService $areaService)
    {
        $filter = Filter::instance()
            ->HTML('上级区域：')
            ->input('parent')
            ->HTML('名称：')
            ->input('name')
            ->reset()->submit();
        if ($filter->is_filtering()){
            $list = $areaService->areaList($filter->get_where());
        }else{
            $list = $areaService->areaList();
        }
        return view('admin/areaList',['list'=>$list,'filter'=>$filter]);
    }

    /**
     * 添加区域
     * @param Request $request
     * @param ISystemService $systemService
     */
    public function areaAdd(Request $request,IAreaService $areaService)
    {
        if ($request->isMethod('post')){
            $areaService->areaAdd($request->input('parentid'),$request->input('name'));
        }
        return back();
    }

    /**
     * 编辑区域
     * @param Province $province
     * @param Request $request
     * @param ISystemService $systemService
     */
    public function areaEdit(Request $request,IAreaService $areaService)
    {
        if ($request->isMethod('post')){
            $areaService->areaEdit($request->input('area_id'),$request->input('parentid'),$request->input('name'));
        }
        return back();
    }

    /**
     * 删除区域
     * @param Province $province
     * @param Request $request
     * @param ISystemService $systemService
     */
    public function areaDel(Province $province,Request $request,IAreaService $areaService)
    {
        $areaService->areaDel($province->id);
        return back();
    }

    /**
     * 支付宝日志列表
     * @param Request $request
     */
    public function alipayLogList(Request $request,ISystemService $systemService)
    {
        $filter = Filter::instance()
            ->input('sl_phone')
            ->HTML('记录时间：')
            ->date('add_time')
            ->reset()->submit();
        if ($filter->is_filtering()){
            $list = $systemService->alipayLogList($filter->get_where());
        }else{
            $list = $systemService->alipayLogList();
        }
        return view('admin/alipayLogList',['list'=>$list,'filter'=>$filter]);
    }

    /**
     * 通知事件日志
     * @param Request $request
     */
    public function notifyLogList(Request $request,ISystemService $systemService)
    {
        $filter = Filter::instance()
            ->HTML('记录时间：')
            ->date('add_time')
            ->reset()->submit();
        if ($filter->is_filtering()){
            $list = $systemService->notifyLogList($filter->get_where());
        }else{
            $list = $systemService->notifyLogList();
        }
        return view('admin/notifyLogList',['list'=>$list,'filter'=>$filter]);
    }
}
